﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Threading;
using RestFullChatAJAX.Models;
using Newtonsoft.Json;
using System.Web.Script.Serialization;

namespace RestFullChatAJAX.Controllers
{
    class Params
    {
        public string id;
        public int currentMesIndex, counter;
        public Params(string m, int n, int c) { id = m; currentMesIndex = n; counter = c; }
    }

    public class MessageController : ApiController
    {
        private static List<List<String>> lstMesStore = new List<List<string>>();
        private static object lockMess = new object();
        private bool response = false;
        private object lockBool = new object();
        List<String> lst = new List<String>();

        // GET api/Message/username?3
        public List<String> GetMessage(String id, [FromUri]int currentMesIndex)
        {
            lst.Clear();
            int index = IndexManager.FindStoreIndex(id);
            if (index == -1)
                return null;
            int counter = IndexManager.GetCounter(index);
            if (counter > 20 && currentMesIndex == 0)
                lock (lockMess)
                {
                    lstMesStore[index].RemoveRange(0, counter);
                    counter = 0;
                }
            if (currentMesIndex != counter)
                counter = currentMesIndex;
            bool b = false;
            Params p = new Params(id, currentMesIndex, counter);
            Thread t = new Thread(new ParameterizedThreadStart(functionThread));
            t.Start(p);
            do
            {
                //Interval check notify
                Thread.Yield();                
                lock (lockBool)
                {
                    b = response;
                }
            } while (!b);
            return lst;
        }

        public static void Add()
        {
            lock (lockMess)
            {
                lstMesStore.Add(new List<string>());
            }
        }

        public static void Add(int index, string mess)
        {
            lock (lockMess)
            {
                lstMesStore[index].Add(mess);
            }
        }

        public static void Remove(int index)
        {
            lock (lockMess)
            {
                lstMesStore.RemoveAt(index);
            }
        }

        private List<String> GetNotify(int index, int currentcounter, ref int counter)
        {
            List<String> mess = new List<String>();
            if (index != -1)
            {
                List<String> lst = new List<string>();
                lock (lockMess)
                {
                    lst = lstMesStore[index];
                }
                if (currentcounter < lst.Count())
                {
                    for (int i = currentcounter; i < lst.Count(); i++)
                    {
                        mess.Add(lst[i]);
                        counter++;
                    }
                }
            }
            return mess;
        }

        private void functionThread(object o)
        {
            Params data = (Params)o;
            do
            {
                //Interval check notify
                Thread.Sleep(2000);

                int index = IndexManager.FindStoreIndex(data.id);
                if (index == -1)
                    break;
                lst = GetNotify(index, data.currentMesIndex, ref data.counter);
                if (data.counter != data.currentMesIndex)
                    IndexManager.SetCounter(index, data.counter);
                if (lst.Count > 0)
                    lock (lockBool)
                    {
                        response = true;
                    }
            } while (lst.Count == 0);
        }
    }
}
